<template>
  <el-dialog v-model="dialogFormVisible" :title="title" @close="closeDialog">
    <el-form :model="addBaseWxInformForm" ref="addBaseTeacherCarRef" :rules="addBaseWxInformrules">

      <el-form-item label="姓名" :label-width="formLabelWidth" prop="name">
        <el-input v-model="addBaseWxInformForm.name" autocomplete="off" ref="name" readonly
          placeholder="请填写姓名"></el-input>
      </el-form-item>
      <el-form-item label="学号/工号" :label-width="formLabelWidth" prop="studentNumber">
        <el-input v-model="addBaseWxInformForm.studentNumber" autocomplete="off" ref="name"
          placeholder="请填写学号/工号"></el-input>
      </el-form-item>
      <el-form-item label="用户类型" :label-width="formLabelWidth" prop="studentNumber">
        <el-select v-model="addBaseWxInformForm.userType" placeholder="请选择用户类型">
          <el-option v-for="(item, index) in typeData" :key="index" :label="item.value" :value="item.usertype" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel()">取消</el-button>
        <el-button type="primary" @click="save()">保存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { ref } from "vue";
import { httpService } from "@/utils/httpService";
import { ElMessage, ElForm } from "element-plus";
import { vxRule } from "@/utils/validator";

export default {
  name: "AddBaseTeacherCar",
  components: { ElForm, ElMessage },
  data() {
    return {
      dialogFormVisible: ref(false),
      formLabelWidth: "120px",
      title: "",
      personnelType: "",
      typeData: [
        {
          usertype: 1,
          value: '学生'
        },
        {
          usertype: 2,
          value: '人员'
        },
        {
          usertype: 3,
          value: '其他'
        },
      ],
      addBaseWxInformForm: {
        id: '',
        studentNumber: "",
        name: '',
        userType: '',
      },
      // 校验规则
      addBaseWxInformrules: {
        studentNumber: vxRule(true, null, "blur"),
      },
    };
  },
  methods: {
    closeDialog() {
      this.clear();
    },

    cancel() {
      this.dialogFormVisible = false;
      this.clear();
    },
    clear() {
      this.dialogFormVisible = false;
      //清空form
      this.addBaseWxInformForm.studentNumber = "";
      this.addBaseWxInformForm.id = "";
    },
    save() {
      let params = new FormData()
      params.append('id', this.addBaseWxInformForm.id)
      params.append('studentNumber', this.addBaseWxInformForm.studentNumber)
      params.append('userType', this.addBaseWxInformForm.userType)
      console.log(this.addBaseWxInformForm.id, this.addBaseWxInformForm.studentNumber, this.addBaseWxInformForm.userType);
      httpService("/javaApi/registerInfo/edit", params, "post").then(
        (data) => {
          if (data.code == 200) {
            ElMessage.success("修改成功");
            this.clear();
            //刷新父组件的数据
            this.$parent.getBaseWxInformList();
          } else {
            ElMessage.error("修改失败");
          }
        }
      );
    },
    // save() {
    //   this.$refs.addBaseTeacherCarRef.validate((valid) => {
    //     if (valid) {
    //       let params = new FormData(); //创建一个form对象,必须是form对象否则后端接受不到数据
    //       params.append("teacherName", this.addBaseTeacherCarForm.teacherName);
    //       let arr = this.addBaseTeacherCarForm.teacherName.split("-");
    //       params.append(
    //         "teacherNumber",
    //         arr[1]
    //       );
    //       params.append(
    //         "registerPlate",
    //         this.addBaseTeacherCarForm.registerPlate
    //       );
    //       params.append("clientName", this.addBaseTeacherCarForm.teacherName);
    //       params.append(
    //         "passTypeName",
    //         "固定车"
    //       );
    //       params.append("deadline", "2050-09-01");
    //       params.append("clientNo", this.addBaseTeacherCarForm.clientNo);
    //       params.append("campus", this.addBaseTeacherCarForm.campus);
    //       if (this.addBaseTeacherCarForm.id) {
    //         params.append("id", this.addBaseTeacherCarForm.id);
    //         httpService("/javaApi/baseTeacherCar/edit", params, "post").then(
    //           (data) => {
    //             if (data.code == 200) {
    //               ElMessage.success("修改成功");
    //               this.clear();
    //               //刷新父组件的数据
    //               this.$parent.getBaseTeacherCarList();
    //             } else {
    //               ElMessage.error("修改失败");
    //             }
    //           }
    //         );
    //       } else {
    //         httpService("/javaApi/baseTeacherCar/add", params, "post").then(
    //           (data) => {
    //             if (data.code == 200) {
    //               ElMessage.success("添加成功");
    //               this.clear();
    //               //刷新父组件的数据
    //               this.$parent.getBaseTeacherCarList();
    //             } else {
    //               ElMessage.error("添加失败");
    //             }
    //           }
    //         );
    //       }
    //     } else {
    //       return false;
    //     }
    //   });
    // },
  },
};
</script>
<style>
.el-select {
  width: 100%;
}
</style>